package com.doit.day01;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;

public class _04_创建一张表 {
    public static void main(String[] args) throws IOException {
        Configuration conf = HBaseConfiguration.create();
        //设置参数
        conf.set("hbase.zookeeper.quorum","linux01:2181,linux02:2181,linux03:2181");
        //创建连接对象
        Connection conn = ConnectionFactory.createConnection(conf);
        //获取连接对象
        Admin admin = conn.getAdmin();
        //因为创建表的描述器构建器需要一个表名TableName对象
        TableName tableName = TableName.valueOf("test:teacher");
        //创建一个表的描述器构建器
        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
        //构建列族描述器，就需要先构建一个列族描述器的构建器
        ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder("base_info".getBytes(StandardCharsets.UTF_8));
        //设置一些参数
//        columnFamilyDescriptorBuilder.setTimeToLive(20);
//        columnFamilyDescriptorBuilder.setInMemory(false);
        ColumnFamilyDescriptor build1 = columnFamilyDescriptorBuilder.build();

        ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder1 = ColumnFamilyDescriptorBuilder.newBuilder("extra_info".getBytes(StandardCharsets.UTF_8));
        ColumnFamilyDescriptor build2 = columnFamilyDescriptorBuilder1.build();
        //添加列族的相关信息
//        tableDescriptorBuilder.setColumnFamily(build1);
//        tableDescriptorBuilder.setColumnFamily(build2);
        //整一个集合，将多个列族描述器丢进去
        ArrayList<ColumnFamilyDescriptor> list = new ArrayList<>();
        list.add(build1);
        list.add(build2);
        //设置列族
        tableDescriptorBuilder.setColumnFamilies(list);
        //创建表需要表的描述器
        TableDescriptor build = tableDescriptorBuilder.build();

        //创建一张表
        admin.createTable(build);

    }
}
